package net.luohuasheng.bee.proxy.mybatis.executor.page;

import java.util.Locale;

public class Order {
    private Direction direction;
    private String property;

    public Order(String property, Direction direction) {
        this.property = property;
        this.direction = direction;
    }


    public Direction getDirection() {
        return direction;
    }

    public String getProperty() {
        return property;
    }

    public enum Direction {
        /**
         * 升序 0-9
         */
        ASC,
        /**
         * 降序 9-0
         */
        DESC;

        public boolean isAscending() {
            return this.equals(ASC);
        }

        public boolean isDescending() {
            return this.equals(DESC);
        }

        public static Direction fromString(String value) {
            try {
                return Direction.valueOf(value.toUpperCase(Locale.US));
            } catch (Exception e) {
                throw new IllegalArgumentException(String.format(
                        "Invalid value '%s' for orders given! Has to be either 'desc' or 'asc' (case insensitive).", value), e);
            }
        }

        public static Direction fromStringOrNull(String value) {
            try {
                return fromString(value);
            } catch (IllegalArgumentException e) {
                return null;
            }
        }
    }

    public static Order of(String property) {
        return new Order(property, Direction.ASC);
    }

    public static Order of(String property, Direction direction) {
        return new Order(property, direction);
    }
}
